package com.smdk.dsminio.tasks;

import cn.hutool.log.StaticLog;
import com.alibaba.fastjson.JSON;
import com.smdk.dsminio.entity.DsminioServerNode;
import com.smdk.dsminio.redis.RedisUtil;
import com.smdk.dsminio.service.DsminioServerNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.Comparator;
import java.util.List;

@Configuration
@EnableScheduling
public class FileNodeInfoSynchronizedScheduleTask {
    @Autowired
    private DsminioServerNodeService dsminioServerNodeService;
    @Autowired
    private RedisUtil REDISUTILS;
    //十秒执行一次节点信息同步
    @Scheduled(fixedRate=10000)
    private void fileNiodeInfoTasks() {
        List<DsminioServerNode> dsminioServerNodeList = dsminioServerNodeService.list();
        //对Node信息进行排序
        dsminioServerNodeList.sort(Comparator.comparing(DsminioServerNode::getNodeDiskSize));
        REDISUTILS.set("nodeInfo", JSON.toJSONString(dsminioServerNodeList));
        StaticLog.info("节点信息同步完毕：{}",JSON.toJSONString(dsminioServerNodeList));
    }
}